/**
 * Copyright (C), 2019-2022, 宇信融汇
 * FileName: DeadLetterConfig
 * Author:   666
 * Date:     2022/4/22 7:49
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 666           修改时间           版本号              描述
 */
package com.mashibing.rabbitmq_boot.config;

import org.springframework.amqp.core.*;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Classname DeadLetterConfig
 * @Description 死心队列的config
 *              1、获取交换机
 *              2、获取队列
 *              3、通过路由绑定
 * @Date 2022/4/22 7:49
 * @Created by 666
 */
@Configuration
public class DeadLetterConfig {
    public static final String NORMAL_EXCHANGE = "normal-exchange";
    public static final String NORMAL_QUEUE = "normal-queue";
    public static final String NORMAL_ROUTING_KEY = "normal.#";

    public static final String DEAD_EXCHANGE = "dead-exchange";
    public static final String DEAD_QUEUE = "dead-queue";
    public static final String DEAD_ROUTING_KEY = "dead.#";

    @Bean
    public Exchange normalExchange() {
        return ExchangeBuilder.topicExchange(NORMAL_EXCHANGE).build();
    }

    /**
     *@Description .deadLetterExchange(DEAD_EXCHANGE).deadLetterRoutingKey(DEAD_ROUTING_KEY) 是为了队列绑定死心交换机然后路由到死心队列
     *              durable队列持久化，如果重启丢失
     *@return org.springframework.amqp.core.Queue
     *@date
     *@auther
     */
    @Bean
    public Queue normalQueue() {
        return QueueBuilder.durable(NORMAL_QUEUE).deadLetterExchange(DEAD_EXCHANGE).deadLetterRoutingKey("dead.abc").maxLength(1).build();
    }

    @Bean
    public  Binding  normalBinding(Exchange normalExchange,Queue normalQueue){
        return BindingBuilder.bind(normalQueue).to(normalExchange).with(NORMAL_ROUTING_KEY).noargs();
    }

    @Bean
    public Exchange deadExchange() {
        return ExchangeBuilder.topicExchange(DEAD_EXCHANGE).build();
    }

    /**
     *@Description
     *@return org.springframework.amqp.core.Queue
     *@date
     *@auther
     */
    @Bean
    public Queue deadQueue() {
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    @Bean
    public  Binding  deadBinding(Exchange deadExchange,Queue deadQueue){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY).noargs();
    }


}
